(function (vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 10;
    vc.extends({
        data: {
            productInfo: {
                goodsClass: {},
                products: [],
                classPath: [],
                records: '',
                pages: []
            },
            minPrice: '',
            maxPrice: '',
            searchKey:'',
            state: '2002'
        },
        _initMethod: function () {
            $that.searchKey = vc.getParam("searchKey");
            if (vc.getParam("hktId")) {
                $that._queryProductByCondition(vc.getParam("hktId"), DEFAULT_PAGE, DEFAULT_ROWS);
                $that._queryClassByClassId(vc.getParam("hktId"));
            } else {
                $that.minPrice = vc.getParam("minPrice");
                $that.maxPrice = vc.getParam("maxPrice");
                $that._queryProductByCondition('', DEFAULT_PAGE, DEFAULT_ROWS);
                $that._queryClassByClassId();
            }
            $that.websiteAccessLog();
        },
        _initEvent: function () {
            vc.on('pagination', 'page_event', function (_currentPage) {
                vc.component._queryProductByCondition(vc.getParam("hktId"), _currentPage, DEFAULT_ROWS);
            });
        },
        methods: {
            _queryClassByClassId: function (_hktId) {
                let param = {
                    params: {
                        page: -1,
                        row: 100,
                        hktId: _hktId
                    }
                };
                vc.http.apiGet(
                    'housekeepingType.queryHousekeepingType',
                    param,
                    function (json, res) {
                        let _classInfo = JSON.parse(json);
                        if (!_hktId) {
                            $that.productInfo.goodsClass.children = _classInfo.data;
                            return;
                        }
                        $that.productInfo.goodsClass = _classInfo.data[0];
                        //$that._queryClassesByClassIdPath(_classInfo.data[0].classIdPath)
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _queryClassesByClassIdPath: function (_classIdPath) {
                let param = {
                    page: -1,
                    row: 100,
                    classIds: _classIdPath
                };
                vc.http.apiPost(
                    'goodsClass.listGoodsClasses',
                    JSON.stringify(param),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _classesInfo = JSON.parse(json);
                        let classPath = [];
                        let classIdPath = _classIdPath.split("-");
                        for (let i = 0; i < classIdPath.length; i++) {
                            for (let j = 0; j < _classesInfo.data.length; j++) {
                                if (classIdPath[i] === _classesInfo.data[j].classId) {
                                    classPath.push(_classesInfo.data[j]);
                                }
                            }
                        }
                        $that.productInfo.classPath = classPath;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _queryProductByCondition: function (_classId, _page, _rows) {
                let param = {
                    params: {
                        page: _page,
                        row: _rows,
                        hktId: _classId,
                        minPrice: $that.minPrice,
                        maxPrice: $that.maxPrice,
                        prodNameLike: $that.searchKey,
                        //state: $that.state,
                    }
                };
                vc.http.apiGet(
                    'product.queryProduct',
                    param,
                    function (json, res) {
                        let _productInfo = JSON.parse(json);
                        $that.productInfo.total = _productInfo.total;
                        $that.productInfo.records = _productInfo.records;

                        vc.emit('pagination', 'init', {
                            total: vc.component.productInfo.records,
                            currentPage: _page,
                            totalItem: _productInfo.total
                        });

                        /*var records = _productInfo.records;
                        var _records = [];
                        if (records > 1) {
                            for (let i = 1; i <= records; i++) {
                                _records.push(i);
                            }
                            $that.productInfo.pages = _records;
                        } else {
                            $that.productInfo.pages = _records;
                        }*/
                        $that.productInfo.products = _productInfo.data;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _jumpToPageByClassId: function (_classId) {
                vc.jumpToPage("/page.html#/pages/plateform/product?classId=" + _classId);
            },
            _jumpToPageByPrice: function () {
                if ($that.minPrice || $that.maxPrice) {
                    $that._queryProductByCondition(vc.getParam("hktId"),  DEFAULT_PAGE, DEFAULT_ROWS);
                }
            },
            _jumpToPageByCancel: function (_type) {
                let minPrice = $that.minPrice;
                let maxPrice = $that.maxPrice;
                switch (_type) {
                    case 'class':
                        vc.jumpToPage("/page.html#/pages/plateform/product?minPrice=" + minPrice + "&maxPrice=" + maxPrice);
                        break;
                    case 'price':
                        $that.minPrice = '';
                        $that.maxPrice = '';
                        $that._queryProductByCondition(vc.getParam("hktId"), DEFAULT_PAGE, DEFAULT_ROWS);
                        break;
                }

            },
            _jumpToProductDetail: function (_productId) {
                vc.jumpToPage("/page.html#/pages/plateform/productDetail?productId=" + _productId);
            },
            _queryProductByTypes: function (_type) {
                switch (_type) {
                    case 'default':
                        $that._queryProductByCondition(vc.getParam("hktId"), DEFAULT_PAGE, DEFAULT_ROWS);
                        break;
                    case 'sales':


                }
            },
            _switchPage: function (_page) {
                $that.page = _page;
                $that._queryProductByCondition(vc.getParam("hktId"), DEFAULT_PAGE, DEFAULT_ROWS);
            },
            _addToCart: function (_product) {
                vc.http.apiPost(
                    'storeCart.saveStoreCart',
                    JSON.stringify({
                        prodName:_product.prodName,
                        productId:_product.productId,
                        valueId:_product.valueId,
                        storeId:_product.storeId,
                        price:_product.price,
                        shopId:_product.shopId,
                        cartNum:"1",
                    }), {
                        emulateJSON: true
                    },
                    function(json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            vc.toast("加入购物车成功");
                            return;
                        }else if(_json.code == 404){
                            vc.toast(_json.msg);
                        }else{
                            vc.toast(_json.data);
                        }
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _buyNow:function(_product){
                vc.http.apiPost(
                    'storeCart.saveStoreCart',
                    JSON.stringify({
                        prodName:_product.prodName,
                        productId:_product.productId,
                        valueId:_product.valueId,
                        storeId:_product.storeId,
                        price:_product.price,
                        shopId:_product.shopId,
                        cartNum:"1",
                    }), {
                        emulateJSON: true
                    },
                    function(json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            vc.toast("加入购物车成功");
                            //vc.jumpToPage("/pageLR.html#/pages/plateform/order?productId=" + _product.productId);
                            vc.jumpToPage('/pageLR.html#/pages/plateform/order?productId='+_product.productId+'&shopId='+_product.shopId+'&valueId='+_product.valueId+'&goodsNum=1');
                            return;
                        }else if(_json.code == 404){
                            vc.toast(_json.msg);
                        }else{
                            vc.toast(_json.data);
                        }
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            websiteAccessLog: function () {
                let user = vc.getData('/nav/getUserInfo');
                let param = '';
                if (user) {
                    param = {
                        loginName: user.name,
                        loginId: user.userId,
                        accessUrl: window.location.href,
                        loginState: '1'
                    }
                } else {
                    param = {
                        accessUrl: window.location.href,
                        loginState: '0'
                    }
                }
                vc.http.apiPost('websiteAccessLog.saveWebsiteAccessLog',
                    JSON.stringify(param), {
                        emulateJSON: true
                    },
                    function(json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            return;
                        }
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            }
        }
    });

})(window.vc);
